using System;

using VCDepend.Engine;
using VCDepend.Engine.DependencySources;
using System.IO;
using System.Runtime.InteropServices;

namespace VCDepend.Console
{
	public class Program
	{
		[STAThread]
		static int Main(string[] arguments)
		{
      try
      {
        CommandArgs args = CommandArgs.Parse(arguments);

        Solution solution = SolutionReader.CreateFromFilename(args.SolutionPath);
        ProjectStore projects = solution.GetProjects(new VcProjParser(), args.SolutionConfiguration);

        DependencyGraphBuilder graphBuilder = MakeDependencyGraphBuilder(solution, projects);
        DependencyGraph graph = graphBuilder.BuildGraph(projects);

        DotBuilder resultBuilder = new DotBuilder(args.SolutionName);
        graph.Traverse(resultBuilder);

        resultBuilder.WriteTo(System.Console.Out);
      }
      catch (Exception e)
      {
        System.Console.Error.WriteLine(ExceptionParser.GetErrorMessage(e));
        return ExceptionParser.GetReturnCode(e);
      }

			return 0;
		}

    private static DependencyGraphBuilder MakeDependencyGraphBuilder(Solution solution, ProjectStore projects)
    {
      DependencyGraphBuilder builder = new DependencyGraphBuilder();
      builder.AddDependencySource(new SolutionDependencySource(solution, projects));
      builder.AddDependencySource(new LinkDependencySource());

      return builder;
    }
	}
}
